package com.caszx.chargingpile.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;

import com.alibaba.fastjson.JSONException;
import com.app.base.adapter.CommonViewPageAdapter;
import com.app.base.app.BaseTopBarNetActivity;
import com.app.utils.DiskUser;
import com.app.view.PagerSlidingTabStrip;
import com.caszx.chargingpile.R;
import com.caszx.chargingpile.fragment.user.UserOrderChargeFragment;
import com.caszx.chargingpile.http.ConstantUrl;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import http.okhttp.OkHttpUtils;
import http.okhttp.callback.JsonCallback;
import okhttp3.Call;

/**
 * 用户订单
 * */
public class UserHistoryOrderActivity extends BaseTopBarNetActivity {


    private List<Fragment> pagers = new ArrayList<Fragment>(2);
    private CommonViewPageAdapter mAdapter;
    private ViewPager viewpager;
    private PagerSlidingTabStrip viewpageTabs;

    @Override
    protected void onLoad() {
        HashMap<String, String> params = new HashMap<>();
        params.put("token", DiskUser.getStringValueByKey(this, "token"));
        OkHttpUtils.post()
                .url(ConstantUrl.URL_USER_ORDERS)
                .params(params)
                .tag(this)
                .build()
                .execute(new JsonCallback() {
                    @Override
                    public void onError(Call call, Exception e) {
                        showErrorView(R.string.error_network);
                    }

                    @Override
                    public void onResponse(JSONObject response) {
                        JSONObject resultJsonObject = response.optJSONObject("result");
                        if (resultJsonObject != null) {
                            int code = resultJsonObject.optInt("code");
                            if (code < 0) {
                                //error
                                // DialogHelp.showDialogOnlyMesg(resultJsonObject.optString("msg"), null);
                                showErrorView(resultJsonObject.optString("msg"));
                            } else {
                                try {
                                    showContentView();
                                    findView(response);
                                } catch (JSONException e) {
                                    showErrorView(R.string.error_parser_json);
                                }
                            }
                        }
                    }
                });
    }

    @Override
    protected View initOnloadedView() {
        View view = getLayoutInflater().inflate(R.layout.activity_user_history_order, null);
        return view;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setCenterTitle("订单记录");
    }

    protected void findView(JSONObject response) {
        //预约总费用
        double storeupFees = response.optDouble("storeupFees");
        //预约记录
        String str_storeupOrders = response.optString("storeupOrders");

        //充电总费用
        double chargeFees = response.optDouble("chargeFees");
        //充电记录
        String str_chargeOrders= response.optString("chargeOrders");

        //隐藏预约订单
//        Fragment fragment0 = UserOrderReservateFragment.newInstance(storeupFees,str_storeupOrders);
        Fragment fragment1 = UserOrderChargeFragment.newInstance(chargeFees,str_chargeOrders);
//        pagers.add(fragment0);
        pagers.add(fragment1);

        viewpager = (ViewPager)findViewById(R.id.viewpager);
        viewpager.setOffscreenPageLimit(pagers.size());
        mAdapter = new CommonViewPageAdapter(getSupportFragmentManager());
        mAdapter.setData(pagers);
        String[] tabs = getResources().getStringArray(R.array.reservate_order_tab);
        mAdapter.setPageTitles(tabs);
        viewpager.setAdapter(mAdapter);
        viewpageTabs = (PagerSlidingTabStrip)findViewById(R.id.indicator_tab);
        setTabsValue(viewpager);
    }

    protected void setTabsValue(ViewPager viewpage) {
        DisplayMetrics dm = getResources().getDisplayMetrics();
        viewpageTabs.setViewPager(viewpage);
        // 设置Tab是自动填充满屏幕的
        viewpageTabs.setShouldExpand(true);
        // 设置Tab的分割线是透明的
        viewpageTabs.setDividerColor(Color.TRANSPARENT);
        // 设置Tab底部线的高度
        viewpageTabs.setUnderlineHeight((int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, 1, dm));
        // 设置Tab Indicator的高度
        viewpageTabs.setIndicatorHeight((int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, 2, dm));
        // 设置Tab标题文字的大小
        viewpageTabs.setTextSize((int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_SP, 14, dm));
        // 设置Tab Indicator的颜色
        viewpageTabs.setIndicatorColor(getResources().getColor(R.color.colorAccent));
        // 设置选中Tab文字的颜色
        viewpageTabs.setSelectedTextColor(getResources().getColor(R.color.colorAccent));
        // 取消点击Tab时的背景色
        viewpageTabs.setTabBackground(0);
        viewpageTabs.setBackgroundColor(getResources().getColor(R.color.white));
    }

}
